import Ember from 'ember';
import InfiniteScroll from '../../infinite-scroll';

export default Ember.Controller.extend(InfiniteScroll,{
  infiniteContentPropertyName: "userMyInfoItemEdit",
  infiniteModelName: "user",
  infiniteContainerName:"userMyInfoItemEditContainer",


  feedService: Ember.inject.service('feed-bus'),
  editEnd:true,
  backRoutePath:null,//返回路径
  curRoutePath: null,//当前路径
  isSquare:true,

  isMobile: Ember.computed(function () {
    console.log("isMobile in:", this.get("global_curStatus").get("isMobile"));
    return this.get("global_curStatus").get("isMobile");
  }),

  //通过event service监控顶部菜单的选择事件，并进行相关方法调用
  listenner: function() {
    this.get('feedService').on('saveFun_save', this, 'save');
  }.on('init'),
  ageRegular: Ember.computed(function(){
    var num=this.get('edit');
    var Reg= /[1-9]?[0-9]|100/;
    return this.Regular(num,Reg);
  }).property('edit'),
  phoneRegular: Ember.computed(function(){
    var num=this.get('edit');
    var Reg= /^1\d{10}/;
    return this.Regular(num,Reg);
  }).property('edit'),
  emailRegular:Ember.computed(function(){
    var num=this.get('edit');
    var Reg=  /^\w+@[a-zA-Z0-9]+\.[a-zA-Z]+$/;
    return this.Regular(num,Reg);
  }).property('edit'),

  Regular:function(num,Reg){
    if (num && !(Reg.test(num))) {
      return false;
    }
    return true;
  },
  maxlength: Ember.computed("source",function () {
    var maxlength = "";
    var source = this.get("source");
    if(source === 'email'){
      maxlength ="33";
    }
    if(source === 'phone'){
      maxlength = '11';
    }
    if(source === 'weixin'){
      maxlength = '33';
    }
    return maxlength;
  }),
  editPlaceholder: Ember.computed("source",function () {
    var editPlaceholder = "";
    var source = this.get("source");
    if(source === 'name'){
      editPlaceholder ="请输入用户名";
    }
    if(source === 'name'){
      editPlaceholder ="请输入年龄";
    }
    if(source === 'email'){
      editPlaceholder ="请输入邮箱账号";
    }
    if(source === 'phone'){
      editPlaceholder = '请输入手机号码';
    }
    if(source === 'weixin'){
      editPlaceholder = '请输入微信账号';
    }
    return editPlaceholder;
  }),
  /**
  * 保存——钩：调用Fun()方法；
  * code
  * lishan
  * 2016-8-4
  */
  save() {
    var _self=this;
    console.log("this.edit",_self.get("edit"));
    var curUser = this.get("global_curStatus").getUser();
    this.store.findRecord("userPrivate",curUser.get("id")).then(function(userEnt){
      userEnt.set("introduce",_self.get("model.curUser.introduce"));
      userEnt.save();
    });
    if(_self.get('source')==="email" && !this.get('emailRegular')){
      this.set('succeed','请输入正确的邮箱账号！');
      console.log("Invalid format in email!");
    }else if (_self.get('source')==="phone" && !this.get('phoneRegular')) {
      this.set('succeed','请输入正确的手机号码！');
      console.log("Invalid format in phone!");
    }else if (_self.get('source')==="age" && !this.get('ageRegular')) {
      this.set('succeed','请输入正确0-100的年龄！');
      console.log("Invalid format in age!");
    }
    else if ( !(_self.get("edit"))){
      console.log('_self.get in edit is none ,not do save');
      var mainController = App.lookup("controller:business.mainpage");
      mainController.switchMainPage("business-mine");
    }
    else {
      this.store.findRecord("userPrivate",curUser.get("id")).then(function(userEnt){
        if(_self.get('source')==="email"){
          console.log(' email ======= ',userEnt.get("email"));
          userEnt.set("email",_self.get("edit"));
          console.log(' email111111=======',userEnt.get("email"));
        }else if (_self.get('source')==="phone") {
          console.log('  phone ====== ',userEnt.get("phone"));
          userEnt.set("phone",_self.get("edit"));
          console.log('  phone 1111111======= ',userEnt.get("phone"));
        }else if (_self.get('source')==="weixin"){
          console.log('  weixin ====== ',userEnt.get("weixin"));
          userEnt.set("weixin",_self.get("edit"));
          console.log(' weixin 1111111===== ',userEnt.get("weixin"));
        }else if (_self.get('source')==="name")  {
          userEnt.set("name",_self.get("edit"));
        }else if (_self.get('source')==="age") {
          userEnt.set("age",_self.get("edit"));
        console.log(' weixin 1111111===== ',userEnt.get("age"));
        }
        userEnt.save().then(function(ll){
          var mainController = App.lookup("controller:business.mainpage");
          mainController.switchMainPage("business-mine");
          console.log('  email22222=====',ll.get("email"));
          console.log('  phone22222=====',ll.get("phone"));
          console.log('  weixin22222======',ll.get("weixin"));
          console.log('  weixin22222======',ll.get("age"));
        });
      });
        // _self.get('target').send("reloadModel");
    }
  },
});
